package com.ruoyi.model.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.util.IdUtil;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.model.domain.ModelAlgorithmParam;
import com.ruoyi.model.domain.vo.ModelAlgorithmVo;
import com.ruoyi.model.service.IModelAlgorithmParamService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.model.mapper.ModelAlgorithmMapper;
import com.ruoyi.model.domain.ModelAlgorithm;
import com.ruoyi.model.service.IModelAlgorithmService;

/**
 * 模型管理Service业务层处理
 *
 * @author rww
 * @date 2024-11-25
 */
@Service
public class ModelAlgorithmServiceImpl implements IModelAlgorithmService
{
    @Autowired
    private ModelAlgorithmMapper modelAlgorithmMapper;

    @Autowired
    private IModelAlgorithmParamService modelAlgorithmParamService;

    /**
     * 查询模型管理
     *
     * @param id 模型管理主键
     * @return 模型管理
     */
    @Override
    public ModelAlgorithm selectModelAlgorithmById(Long id)
    {
        return modelAlgorithmMapper.selectModelAlgorithmById(id);
    }

    /**
     * 查询模型管理列表
     *
     * @param modelAlgorithm 模型管理
     * @return 模型管理
     */
    @Override
    public List<ModelAlgorithm> selectModelAlgorithmList(ModelAlgorithm modelAlgorithm)
    {
        return modelAlgorithmMapper.selectModelAlgorithmList(modelAlgorithm);
    }

    /**
     * 新增模型管理
     *
     * @param modelAlgorithm 模型管理
     * @return 结果
     */
    @Override
    public int insertModelAlgorithm(ModelAlgorithm modelAlgorithm)
    {
        modelAlgorithm.setId(IdUtil.getSnowflakeNextId());
        modelAlgorithm.setCreateTime(DateUtils.getNowDate());
        return modelAlgorithmMapper.insertModelAlgorithm(modelAlgorithm);
    }

    /**
     * 修改模型管理
     *
     * @param modelAlgorithm 模型管理
     * @return 结果
     */
    @Override
    public int updateModelAlgorithm(ModelAlgorithm modelAlgorithm)
    {
        modelAlgorithm.setUpdateTime(DateUtils.getNowDate());
        return modelAlgorithmMapper.updateModelAlgorithm(modelAlgorithm);
    }

    /**
     * 批量删除模型管理
     *
     * @param ids 需要删除的模型管理主键
     * @return 结果
     */
    @Override
    public int deleteModelAlgorithmByIds(Long[] ids)
    {
        return modelAlgorithmMapper.deleteModelAlgorithmByIds(ids);
    }

    /**
     * 删除模型管理信息
     *
     * @param id 模型管理主键
     * @return 结果
     */
    @Override
    public int deleteModelAlgorithmById(Long id)
    {
        ModelAlgorithmParam modelAlgorithmParam=new ModelAlgorithmParam();
        modelAlgorithmParam.setAlgorithmId(id);
        List<ModelAlgorithmParam> list = modelAlgorithmParamService.selectModelAlgorithmParamList(modelAlgorithmParam);
        if(ObjectUtils.isNotEmpty(list)){
            List<Long> ids=list.stream().map(ModelAlgorithmParam::getId).collect(Collectors.toList());
            modelAlgorithmParamService.deleteModelAlgorithmParamByIds(ids.stream().toArray(Long[]::new));
        }
        return modelAlgorithmMapper.deleteModelAlgorithmById(id);
    }

    @Override
    public ModelAlgorithmVo selectModelAlgorithmVoById(Long id) {
        ModelAlgorithm info=modelAlgorithmMapper.selectModelAlgorithmById(id);
        ModelAlgorithmVo algorithm= BeanUtil.copyProperties(info,ModelAlgorithmVo.class);
        if(ObjectUtils.isNotEmpty(algorithm)){
            ModelAlgorithmParam modelAlgorithmParam=new ModelAlgorithmParam();
            modelAlgorithmParam.setAlgorithmId(algorithm.getId());
            List<ModelAlgorithmParam> list = modelAlgorithmParamService.selectModelAlgorithmParamList(modelAlgorithmParam);
            algorithm.setParamList(list);
        }
        return algorithm;
    }

    @Override
    public int setModelAlgorithm(ModelAlgorithmVo modelAlgorithm) {
        ModelAlgorithm info=modelAlgorithmMapper.selectModelAlgorithmById(modelAlgorithm.getId());
        ModelAlgorithmVo algorithm= BeanUtil.copyProperties(info,ModelAlgorithmVo.class);
        if(ObjectUtils.isNotEmpty(algorithm)){
            ModelAlgorithmParam modelAlgorithmParam=new ModelAlgorithmParam();
            modelAlgorithmParam.setAlgorithmId(algorithm.getId());
            List<ModelAlgorithmParam> list = modelAlgorithmParamService.selectModelAlgorithmParamList(modelAlgorithmParam);
            if(ObjectUtils.isNotEmpty(list)){
                List<Long> ids=list.stream().map(ModelAlgorithmParam::getId).collect(Collectors.toList());
                modelAlgorithmParamService.deleteModelAlgorithmParamByIds(ids.stream().toArray(Long[]::new));
            }
            for (ModelAlgorithmParam param:modelAlgorithm.getParamList()){
                param.setAlgorithmId(algorithm.getId());
                modelAlgorithmParamService.insertModelAlgorithmParam(param);
            }
        }
        return 1;
    }
}
